<?php
namespace api\toilet\controller;

use cmf\controller\RestBaseController;

class RealController extends RestBaseController
{

    /**
     * [getAttendanceData 考勤数据接受]
     * @return [type] [description]
     */
    public function getRealData()
    {
        $mac = $this->request->param('mac');

        $redis = new \Redis();
        $redis->connect('127.0.0.1', 6379);
        $result = $redis->hgetall('cloud_' . $mac); //
        if (!empty($result))
        {
            foreach ($result as $key => $value)
            {
                $toiletTmp = @unserialize($value);
                if (!empty($toiletTmp))
                {
                    foreach ($toiletTmp as $k => $v)
                    {

                        if (!empty($k))
                        {
                            $tmp = @unserialize($v);
                            if (substr_count($key, 'device'))
                            {
                                $toilet[$key][$k]['zsta']  = isset($tmp['zsta']) ? $tmp['zsta'] : 0;
                                $toilet[$key][$k]['order'] = $tmp['order'];
                            }
                            else
                            {
                                $toilet[$key][$k] = $tmp;
                            }
                        }
                    }
                }
            }
            foreach ($toilet as $key => $value)
            {
                if (substr_count($key, 'device'))
                {
                    @array_multisort(array_column($value, 'order'), SORT_ASC, $value);
                }
                $keys           = str_replace('cloud_' . $mac . '_', '', $key);
                $toilets[$keys] = array_values($value);
            }
            return $toilet;
        }

    }

}
